package com.icebartech.base.modules.common.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.icebartech.base.modules.common.mapper.AppAttachmentInfoMapper;
import com.icebartech.base.modules.common.model.AppAttachmentInfo;
import com.icebartech.base.modules.common.service.AttachmentInfoService;
import com.icebartech.core.components.AliyunOSSComponent;
import com.icebartech.core.conf.SiteProperties;
import com.icebartech.core.modules.AbstractService;
import com.icebartech.core.modules.BaseMapper;
import com.icebartech.core.utils.BeanMapper;
import com.icebartech.core.vo.FileInfo;

@Service
public class AttachmentInfoServiceImpl extends AbstractService implements AttachmentInfoService {

	@Autowired
	private AppAttachmentInfoMapper appAttachmentInfoMapper;
	
	@Autowired
	private AliyunOSSComponent aliyunOSSComponent;
	
	@Override
	public FileInfo saveAttachmentFile(FileInfo fileInfo) {
		AppAttachmentInfo info = new AppAttachmentInfo();
		info.setCreator("[SYS]");
		info.setFileKey(fileInfo.getFileKey());
		info.setFileName(fileInfo.getFileName());
		info.setFileUrl(fileInfo.getFileUrl());
		info.setModifier("[SYS]");
		info.setTargetType(fileInfo.getTargetType());
		appAttachmentInfoMapper.insert(info);
		if(info.getId() != null && info.getId() > 0){
			fileInfo.setFileId(info.getId());
		}
		return fileInfo;
	}

	@Override
	public FileInfo findAttachmentFile(String fileId) {
		AppAttachmentInfo info = findById(Long.valueOf(fileId));
		if(info == null){
			return null;
		}
		FileInfo fileInfo = BeanMapper.map(info, FileInfo.class);
		String fileUrl = aliyunOSSComponent.getFileUrl( fileInfo.getFileKey());
		fileInfo.setFileId(info.getId());
		fileInfo.setFileUrl(fileUrl);
		return fileInfo;
	}

	@Override
	public BaseMapper getBaseMapper() {
		return this.appAttachmentInfoMapper;
	}

}
